package com.jidu.api;

import com.jidu.common.config.RuoYiConfig;
import com.jidu.common.core.controller.BaseController;
import com.jidu.common.core.domain.AjaxResult;
import com.jidu.common.utils.file.FileUploadUtils;
import com.jidu.domain.CourseStudentRel;
import com.jidu.service.ICourseStudentRelService;
import com.jidu.service.IStudentClassTimeRelService;
import com.jidu.service.ISysStudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Api(tags = "家长端 controller")
@RestController
@RequestMapping("/app/api")
public class ApiStudentController extends BaseController {

//    @Value("${uploadUrl}")
//    private String uploadUrl;

    @Autowired
    private ICourseStudentRelService courseStudentRelService;
    @Autowired
    private IStudentClassTimeRelService studentClassTimeRelService;
    @Autowired
    private ISysStudentService sysStudentService;

    /**
     * 图片上传
     */
    @PostMapping(value = {"/upload"})
    public AjaxResult upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        if(file.isEmpty()){
            return AjaxResult.error("图片不能为空!");
        }
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            Map<String,String> map = new HashMap<>();
            map.put("fileName", fileName);
//            map.put("url", uploadUrl+fileName);
            return AjaxResult.success(map);
        } catch (Exception e) {
            return AjaxResult.error("上传图片异常，请联系管理员");
        }
    }

    /**
     * 手机号 密码登录
     */
    @PostMapping("/login")
    @ApiOperation(value = "手机号、密码登录")
    public AjaxResult login(@RequestBody Map<String,String> paramsMap){
        return AjaxResult.success(sysStudentService.login(paramsMap));
    }


    /**
     * 修改密码
     */
    @PutMapping("/update/password")
    @ApiOperation(value = "修改登录密码")
    public AjaxResult updatePassword(@RequestParam(value = "oldPassword") String oldPassword,
                                     @RequestParam(value = "newPassword") String newPassword,
                                     @RequestParam(value = "twoNewPassword")String twoNewPassword,
                                     @RequestParam(value = "studentId")Long studentId){
        return AjaxResult.success(sysStudentService.updatePassword(oldPassword,newPassword,twoNewPassword,studentId));
    }


    /**
     * 基础信息列表
     */
    @GetMapping("/getInfo")
    @ApiOperation(value = "个人基础信息列表")
    public AjaxResult getInfo(@RequestParam(value = "studentId")Long studentId){
        return AjaxResult.success(courseStudentRelService.getInfo(studentId));
    }


    /**
     * 课时记录列表
     */
    @ApiOperation(value = "学员上课记录")
    @GetMapping("/classTime/list")
    public AjaxResult classTimeList(@RequestParam(value = "studentId")Long studentId,@RequestParam(value = "courseStudentId")Long courseStudentId){
        return AjaxResult.success(studentClassTimeRelService.classHourRecord(studentId,courseStudentId));
    }
}
